package com.example.demo.response;


import java.io.Serializable;

public class JsonResponse<T> implements Serializable {


    /*返回码*/
    private Integer code;
    /*返回信息提示*/
    private String message;
    /*返回的数据*/
    private T data;

    public JsonResponse() {

    }

    public JsonResponse(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static JsonResponse success() {
        return new JsonResponse(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), "");
    }

    public static JsonResponse success(Object object) {
        return new JsonResponse(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), object);
    }

    public static JsonResponse error() {
        return new JsonResponse(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMsg(), "");
    }


    public static JsonResponse error(Integer errorCode) {
        if (errorCode == 403) {
            return new JsonResponse(errorCode, ResultEnum.AUTHORITY_FAIL.getMsg(), "");
        } else if (errorCode == 422) {
            return new JsonResponse(errorCode, ResultEnum.PARAMETER_ERROR.getMsg(), "");
        } else if (errorCode == 406) {
            return new JsonResponse(errorCode, ResultEnum.REQUEST_TIMEOUNT.getMsg(), "");
        } else if (errorCode == 401) {
            return new JsonResponse(errorCode, ResultEnum.SAFETY_CERTIFICATION_FAIL.getMsg(), "");
        } else {
            return new JsonResponse(errorCode, ResultEnum.ERROR.getMsg(), "");
        }
    }

    //系统自定义异常信息
    public static JsonResponse error(String msg) {
        return new JsonResponse(ResultEnum.ERROR.getCode(), msg, "");
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
